package com.lhj.bootjspshiro.Service.impl;

import com.lhj.bootjspshiro.Service.UserService;
import com.lhj.bootjspshiro.dao.UserDao;
import com.lhj.bootjspshiro.entity.User;
import com.lhj.bootjspshiro.util.SaltUtil;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;

    @Override
    public void register(User user) {
        String salt = SaltUtil.getSalt(6);
        user.setSalt(salt);
        user.setUsername(user.getUsername());
        Md5Hash md5Hash = new Md5Hash(user.getPassword(), salt, 1024);
        user.setPassword(md5Hash.toHex());
        userDao.save(user);
    }

    @Override
    public User findByUserName(String userName) {
        return userDao.findByUserName(userName);
    }
}
